﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GACFFP.Services
{
    public class ValidateVoter
    {

        public ValidateVoter()
        {
           
        }


        /// <summary>
        ///  Every time
        ///  psuedo code
        ///    http://dotnethangout.blogspot.com/2007/02/cookies-in-aspnet.html
        ///        
        ///     GACFFP_Voter = GACFFP user cookie name
        ///     GASF_User = GASF user coming in
        /// </summary>
        /// <returns></returns>
        public static bool IsValidVoter()
        {
            try
            {
                if (HttpContext.Current.Request.Cookies["GACFFP_Voter"] != null)
                {
                    return IsValidFPUser(HttpContext.Current.Request.Cookies["GACFFP_Voter"].Value);
                }
                else if (HttpContext.Current.Request.Cookies["GASF_User"] != null)
                {
                    if (IsValidFPUser(HttpContext.Current.Request.Cookies["GASF_User"].Value))
                    {
                        //insert cookie into GACFFP_Voter cookie.
                        AddGACFFPCookie(HttpContext.Current.Request.Cookies["GASF_User"].Value);
                        return true;
                    }
                    else 
                    {
                        //Create new user in system
				        //I could simplify this but I want to catch a failure
				        CreateUser(DecryptUser(HttpContext.Current.Request.Cookies["GASF_User"].Value));
				
				        //insert cookie into GACFFP_Voter cookie.
				        AddGACFFPCookie(HttpContext.Current.Request.Cookies["GASF_User"].Value);
				
				        return true;
			        }	
		        }
		        else
		        {
			        //future feature
			        //add in functionality for other systems logins
			            //facebook
			            //open id
			            //windows live
			        return false;
		        }
            }
            catch (Exception e)
            {
                //TODO Logging Logic
                return false;
            }
        }



        public static bool IsValidVoter(string GACFFP_Voter)
        {
            try
            {
                if (IsValidFPUser(GACFFP_Voter))
                {
                    //insert cookie into GACFFP_Voter cookie.
                    AddGACFFPCookie(GACFFP_Voter);
                    return true;
                }
                else 
                {
                    return false;
                }
            
            }
            catch (Exception e)
            { 
                //TODO Logging Logic
                return false;
            }
        }



        public static bool VotedThisPeriod()
        {
            //return true;
            
            //set to return false until logic for user check is enabled.
            return false;
        }


            public static bool CreateUser(string plainTextUserName)
            {
                //do not catch error
                //it should bubble up from here.
                try
                {



                    return true;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }


            public static bool AddGACFFPCookie(string EncryptedUserName)
            {
                try
                {
                    HttpCookie objCookie = new HttpCookie("GACFFP_Voter");
                    HttpContext.Current.Response.Cookies.Clear();
                    HttpContext.Current.Response.Cookies.Add(objCookie);
                    objCookie.Values.Add("GACFFP_Voter", EncryptedUserName);
                    DateTime dtExpire = DateTime.Now.AddDays(7);
                    HttpContext.Current.Response.Cookies["GACFFP_Voter"].Expires = dtExpire;

                    return true;
                }
                catch (Exception e)
                {
                    //TODO logging logic
                    return false;
                }
            }


            public static bool IsValidFPUser(string EncryptedUserName)
            {
                try
                {
                    //check session for valid flag
                    //this will stop extra processing and obviate the need to check the db for user.
                    if (HttpContext.Current.Session["IsValidVoter"]!=null)//sessioncheck
                    {
                        if (HttpContext.Current.Session["IsValidVoter"].ToString() == "true")
                        {
                            return true;
                        }
                        else 
                        {
                            //TODO Logging Logic
                            return false;
                        }

                    }
                    else
                    {
                        //unencrypt cookie
                        string user = DecryptUser(EncryptedUserName);
                        //check users in db
                        if (IsUserInSystem(user))
                        {
                            HttpContext.Current.Session["IsValidVoter"] = "true";
                            return true;
                        }
                        else 
                        {
                            return false;
                        }
                    }
                }
                catch (Exception e)
                {
                    //TODO logging logic
                    return false;
                }
            }

            private static bool IsUserInSystem(string user)
            {
                return true;
            }

            //expose as a webservice
            public static string EncryptUser(string plainText)
            {
                //mh2
                string cypherText = plainText;

                return cypherText;


            }


            public static string DecryptUser(string cypherText)
            {
                //mh2
                string plainText = cypherText;
                return plainText;
            }



            public static bool IsUserAdmin(string userName)
            {
                if (IsValidFPUser(userName))
                {
                    var gr = new GACFFP.Models.GACFFP_Repository();

                    GACFFP.User thisUser = gr.GetCurrentUser(userName);

                    if (thisUser.role == "admin")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else 
                { 
                    return false;
                }
            }


    }
}
